package com.dz.service.impl;

import com.dz.constant.MessageConstant;
import com.dz.constant.RedisConstant;
import com.dz.constant.StatusConstant;
import com.dz.enumeration.AppHttpCodeEnum;
import com.dz.exception.SystemException;
import com.dz.redis.RedisCache;
import com.dz.result.Result;
import com.dz.service.ShopService;
import io.jsonwebtoken.lang.Assert;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @author 鄧浩奇
 * @classname ShopServiceImpl
 * @description
 * @since 2024/1/4 19:53
 **/

@Service
public class ShopServiceImpl implements ShopService {

    @Resource
    private RedisCache redisCache;

    /*
     * 店铺营业状态
     * */
    @Override
    public Result<String> shopStatus(Integer status) {
        Assert.notNull(status, AppHttpCodeEnum.DATA_ERROR.getMsg());
        if (!status.equals(StatusConstant.ENABLE) && !status.equals(StatusConstant.DISABLE)) {
            throw new SystemException(AppHttpCodeEnum.DATA_ERROR);
        }
        redisCache.setCacheObject(RedisConstant.SHOP_STATUS, status);
        redisCache.expire(RedisConstant.SHOP_STATUS,1, TimeUnit.DAYS);
        return Result.success();
    }

    /*
     * 获取营业状态
     * */
    @Override
    public Result<Integer> getShopStatus() {
        Object status = redisCache.getCacheObject(RedisConstant.SHOP_STATUS);
        if (Objects.isNull(status) || !(status instanceof Integer s)) {
            return Result.error(MessageConstant.SHOP_STATUS_ERROR);
        } else return Result.success(s);
    }
}
